package MainPack;

import Supporter.GlobalVariables;
import Supporter.ImageSupporter;
import Supporter.SaveLoadSupporter;
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.media.MediaException;
import javax.microedition.midlet.MIDlet;
import javax.microedition.rms.InvalidRecordIDException;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreNotOpenException;

/**
 *
 * @author Mantero
 */
public class SaveCanvas extends AbstractCanvas {

    private static int Exp = 1;
    private static int ExpLvlUp = 1;
    private static int CurrentLv = 1;
    private static int TongueLength = 1;
    private static int Emotion = 1;
    private static int Status = 1;
    private static long Score = 500;
    private static int Stamina =1;
    private long HighScore = 1000;
    private static int Remainpoint = 1;
    private int Money = 1;
    private int Speed = 1;
    private String PetName = "xyz";
    private Timer timer;
    private Image subbackground;
    ImageSupporter is;
    private int count = 0;
    private boolean isCompleted;
    private boolean issaveScene;

    public SaveCanvas(MainMIDlet midlet, boolean isSaveScene) throws RecordStoreException {
        main = midlet;
        Exp = main.getPet().GetAttributes(0);
        Score = main.getPet().getScore();
        HighScore = main.getPet().getHighscore();
        CurrentLv = main.getPet().GetAttributes(1);
        ExpLvlUp = CurrentLv*1000;
        TongueLength = main.getPet().GetAttributes(4);
        Emotion = main.getPet().getEmotion();
        Status = main.getPet().getStatus();
        TongueLength = main.getPet().getTongueLen();
        Stamina = main.getPet().getStamina();
        Money = main.getPet().GetAttributes(5);
        Speed = main.getPet().getSpeech();
        //RecordStore.deleteRecordStore("AmazingPet");
        isCompleted=false;
        SaveLoadSupporter.OpenRecordStore("AmazingPet");
        is = new ImageSupporter();
        background = is.getImage("/save-background1.jpg");
        issaveScene = isSaveScene;
        if (issaveScene) {
            subbackground = is.getImage("/save-load3.png");
            if (SaveLoadSupporter.rs.getNumRecords() > 0) {
                UpdateRecord();
            } else {
                SaveRecord();
            }
        } else {
            if (SaveLoadSupporter.rs.getNumRecords() > 0) {
                LoadRecord();
            }
            subbackground = is.getImage("/loading3.png");
        }

        timer = new Timer();
        timer.schedule(new TimerTask() {

            public void run() {

                if (count == 1) {
                    if (issaveScene) {
                        subbackground = is.getImage("/save-load2.png");
                    } else {
                        subbackground = is.getImage("/loading2.png");
                    }
                    background = is.getImage("/save-background2.jpg");
                    repaint();
                }
                if (count == 2) {
                    if (issaveScene) {
                        subbackground = is.getImage("/save-load1.png");
                    } else {
                        subbackground = is.getImage("/loading1.png");
                    }
                    background = is.getImage("/save-background3.jpg");
                    repaint();
                }
                if (count == 3) {
                        if (isCompleted) {
                            this.cancel();
                        try {
                            main.toPetManamagementScene();
                        } catch (IOException ex) {
                            ex.printStackTrace();
                        } catch (MediaException ex) {
                            ex.printStackTrace();
                        }
                        }
                }
                count++;
            }
        }, 0, 1500);
    }

    public void paint(Graphics g) {
        g.drawImage(background, 0, 0, Graphics.LEFT | Graphics.TOP);
        g.drawImage(subbackground, 30, 20, Graphics.LEFT | Graphics.TOP);
    }

    public void LoadRecord() throws RecordStoreNotOpenException, InvalidRecordIDException, RecordStoreException {
        int value = Integer.parseInt(SaveLoadSupporter.LoadRecord(GlobalVariables.EXP_RECORDID));
        main.getPet().SetAttributes(0, value);
        value = Integer.parseInt(SaveLoadSupporter.LoadRecord(GlobalVariables.EXP_LVLUP_RECORDID));
        main.getPet().setExplvlup(value);
        value = Integer.parseInt(SaveLoadSupporter.LoadRecord(GlobalVariables.CURRENT_LV_RECORDID));
        main.getPet().SetAttributes(1, value);
        value = Integer.parseInt(SaveLoadSupporter.LoadRecord(GlobalVariables.TONGUE_LENGTH_RECORDID));
        main.getPet().setTongueLen(value);
        value = Integer.parseInt(SaveLoadSupporter.LoadRecord(GlobalVariables.EMOTION_RECORDID));
        main.getPet().setEmotion(value);
        value = Integer.parseInt(SaveLoadSupporter.LoadRecord(GlobalVariables.STATUS_RECORDID));
        main.getPet().setStatus(value);
        value = Integer.parseInt(SaveLoadSupporter.LoadRecord(GlobalVariables.SCORE_RECORDID));
        main.getPet().setScore(value);
        value = Integer.parseInt(SaveLoadSupporter.LoadRecord(GlobalVariables.HIGH_SCORE_RECORDID));
        main.getPet().setHighscore(value);
        value = Integer.parseInt(SaveLoadSupporter.LoadRecord(GlobalVariables.REMAIN_POINTS_RECORDID));
        main.getPet().SetAttributes(2, value);
        value = Integer.parseInt(SaveLoadSupporter.LoadRecord(GlobalVariables.MONEY_RECORDID));
        main.getPet().setMoney(value);
        value = Integer.parseInt(SaveLoadSupporter.LoadRecord(GlobalVariables.SPEED_RECORDID));
        main.getPet().setSpeech(value);
        PetName = SaveLoadSupporter.LoadRecord(GlobalVariables.PETNAME_RECORDID);
        main.getPet().setName(PetName);
        //value =  Integer.parseInt(SaveLoadSupporter.LoadRecord(GlobalVariables.STAMINA_RECORDID));
        //main.getPet().setStamina(value);
        isCompleted=true;
    }

    public void SaveRecord() throws RecordStoreNotOpenException, RecordStoreException {
        int kq = 0;
        kq = SaveLoadSupporter.SaveRecord(String.valueOf(Exp));
        kq = SaveLoadSupporter.SaveRecord(String.valueOf(ExpLvlUp));
        kq = SaveLoadSupporter.SaveRecord(String.valueOf(CurrentLv));
        kq = SaveLoadSupporter.SaveRecord(String.valueOf(TongueLength));
        kq = SaveLoadSupporter.SaveRecord(String.valueOf(Emotion));
        kq = SaveLoadSupporter.SaveRecord(String.valueOf(Status));
        kq = SaveLoadSupporter.SaveRecord(String.valueOf(Score));
        kq = SaveLoadSupporter.SaveRecord(String.valueOf(HighScore));
        kq = SaveLoadSupporter.SaveRecord(String.valueOf(Remainpoint));
        kq = SaveLoadSupporter.SaveRecord(String.valueOf(Money));
        kq = SaveLoadSupporter.SaveRecord(String.valueOf(Speed));
        kq = SaveLoadSupporter.SaveRecord(PetName);
        //kq = SaveLoadSupporter.SaveRecord(String.valueOf(Stamina));
        //SaveLoadSupporter.SaveRecord(String.valueOf(Stamina));
        isCompleted = true;
    }

    public void UpdateRecord() throws RecordStoreNotOpenException, InvalidRecordIDException, RecordStoreException {
        SaveLoadSupporter.UpdateRecord(GlobalVariables.EXP_RECORDID, String.valueOf(Exp));
        SaveLoadSupporter.UpdateRecord(GlobalVariables.EXP_LVLUP_RECORDID, String.valueOf(ExpLvlUp));
        SaveLoadSupporter.UpdateRecord(GlobalVariables.CURRENT_LV_RECORDID, String.valueOf(CurrentLv));
        SaveLoadSupporter.UpdateRecord(GlobalVariables.TONGUE_LENGTH_RECORDID, String.valueOf(TongueLength));
        SaveLoadSupporter.UpdateRecord(GlobalVariables.EMOTION_RECORDID, String.valueOf(Emotion));
        SaveLoadSupporter.UpdateRecord(GlobalVariables.STATUS_RECORDID, String.valueOf(Status));
        SaveLoadSupporter.UpdateRecord(GlobalVariables.SCORE_RECORDID, String.valueOf(Score));
        SaveLoadSupporter.UpdateRecord(GlobalVariables.HIGH_SCORE_RECORDID, String.valueOf(HighScore));
        SaveLoadSupporter.UpdateRecord(GlobalVariables.REMAIN_POINTS_RECORDID, String.valueOf(Remainpoint));
        SaveLoadSupporter.UpdateRecord(GlobalVariables.MONEY_RECORDID, String.valueOf(Money));
        SaveLoadSupporter.UpdateRecord(GlobalVariables.SPEED_RECORDID, String.valueOf(Speed));
        SaveLoadSupporter.UpdateRecord(GlobalVariables.PETNAME_RECORDID, PetName);
        //SaveLoadSupporter.UpdateRecord(GlobalVariables.STAMINA_RECORDID, String.valueOf(Stamina));
        isCompleted = true;
    }
}